/** ****************************************************************************
@defgroup CTL_LIGHT CtlLight
@ingroup BSP_External
@brief 色温灯驱动
@addtogroup CtlLight
@{
*******************************************************************************/

#ifndef _TEMP_LIGHT_H_
#define _TEMP_LIGHT_H_

#include "nonos.h"
#include "switch.h"

#ifdef __cplusplus
extern "C" {
#endif


/// 色温定义
#define CTL_TEMP_COOL   20000
#define CTL_TEMP_WARM   0
#define CTL_TEMP_NORMAL 10000

/// 色温灯驱动
typedef struct {
	FUNC_GPIO_PwmSetDuty funcWarmPwmSetDuty;///< GPIO 驱动函数: 暧光修改 PWM 占空比
	FUNC_GPIO_PwmSetDuty funcCoolPwmSetDuty;///< GPIO 驱动函数: 冷光修改 PWM 占空比
	uint16_t lightness;						 	///< 亮度
	uint16_t temperature;						///< 色温：开氏湿度，取值范围0~20000K，值越小越偏暖，越大越偏冷
    OSTaskId blinkTaskId;				 	///< 闪烁任务ID
    uint32_t blinkExOnTime;    				 	///< 闪烁亮的时间
} driver_ctl_light_t;

/**
 * @brief 初始化
 * @param dev 驱动数据结构指针
 * @param funcCoolPwmSetDuty GPIO 驱动函数: 冷光修改 PWM 占空比
 * @param funcWarmPwmSetDuty GPIO 驱动函数: 暧光修改 PWM 占空比
 */
void ctl_light_init(driver_ctl_light_t* dev, FUNC_GPIO_PwmSetDuty funcCoolPwmSetDuty, FUNC_GPIO_PwmSetDuty funcWarmPwmSetDuty);

/// @brief 取得当前亮度
inline static uint16_t ctl_light_get_lightness(driver_ctl_light_t* dev) { return dev->lightness; }
/// @brief 设置亮度
/// @return 状态是否变化了
bool ctl_light_set_lightness(driver_ctl_light_t* dev, uint16_t lightness);

/// @brief 取得当前色温
inline static uint16_t ctl_light_get_temperature(driver_ctl_light_t* dev) { return dev->temperature; }
/// @brief 设置色温
/// @return 状态是否变化了
bool ctl_light_set_temperature(driver_ctl_light_t* dev, uint16_t temperature);

/// @brief 同时设置色温和亮度
/// @return 状态是否变化了
bool ctl_light_set(driver_ctl_light_t* dev, uint16_t temperature, uint16_t lightness);

/// @brief 亮
/// @return 状态是否变化了
inline static bool ctl_light_on(driver_ctl_light_t* dev) { return ctl_light_set_lightness(dev, 0xFFFF); }
/// @brief 灭
/// @return 状态是否变化了
inline static bool ctl_light_off(driver_ctl_light_t* dev) { return ctl_light_set_lightness(dev, 0); }
/// @brief 状态反转
inline static void ctl_light_toggle(driver_ctl_light_t* dev) { ctl_light_get_lightness(dev) ? ctl_light_off(dev) : ctl_light_on(dev); }

/// @brief 开关到指定状态
/// @param state: 0-OFF; 1-ON; 0xFF-REVERSE;
/// @return 状态是否变化了
bool ctl_light_switchTo(driver_ctl_light_t* dev, switch_state_t state, uint32_t keepTime1, uint32_t keepTime2, uint8_t flashTimes);

/// @brief 闪一下
/// @param time: 保持时间, ms
void ctl_light_blink(driver_ctl_light_t* dev, uint16_t time);

/// @brief 闪多下
/// @param onTime: 点亮时间, ms
/// @param offTime: 关闭时间, ms
/// @param count: 闪几次；=OSTASK_REPEAT_FOREVER 表示永远闪烁
/// @retval task, 可通过 osTaskKill() 来停止闪烁
OSTaskId ctl_light_blinkEx(driver_ctl_light_t* dev, uint16_t onTime, uint16_t offTime, uint16_t count);

/// @brief 停止闪烁
inline static void ctl_light_blinkStop(driver_ctl_light_t* dev) { osTaskDestroy(&dev->blinkTaskId); }


#ifdef __cplusplus
}
#endif

#endif  // _TEMP_LIGHT_H_
/// @}
